package com.hongyuan.coach.ui.home.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.hongyuan.coach.R;
import com.hongyuan.coach.base.AppViewModelFactory;
import com.hongyuan.coach.base.CustomActivity;
import com.hongyuan.coach.data.Constants;
import com.hongyuan.coach.data.md5.BaseUtil;
import com.hongyuan.coach.databinding.ActivityStudentsBinding;
import com.hongyuan.coach.ui.home.adapter.StudentsAdapter;
import com.hongyuan.coach.ui.home.beans.StudentsBeans.ListBean.CourseListBean;
import com.hongyuan.coach.ui.home.view_mode.StudentsViewMode;
import me.tatarka.bindingcollectionadapter2.BR;

public class StudentsActivity extends CustomActivity<ActivityStudentsBinding, StudentsViewMode> {

    private String cp_id;
    private String start_date;

    private StudentsAdapter adapter;
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_students;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public StudentsViewMode initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(StudentsViewMode.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        cp_id = getBundle("bundle").getString("cp_id");
        start_date = getBundle("bundle").getString("start_date");

    }

    @Override
    public void initView() {
        super.initView();

        setOnRefresh(binding.refresh,REFRESH_0X3);
        setEnableLoadMore(Constants.PRIVITE_USER_ORDER);
        setEnableRefresh(Constants.PRIVITE_USER_ORDER);
        binding.comBack.setOnClickListener(view -> finish());

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.VERTICAL);
        binding.mRecycler.setLayoutManager(manager);
        adapter = new StudentsAdapter();
        binding.mRecycler.setAdapter(adapter);
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            if(view.getId() == R.id.childBox){
                CourseListBean courseListBean = (CourseListBean) adapter.getData().get(position);

                //清空选择项，并从新选择
                viewModel.clearSelect(courseListBean);
                //刷新
                adapter.notifyDataSetChanged();

                if(BaseUtil.isValue(viewModel.createStr())){
                    viewModel.haveSelect.set(true);
                }else{
                    viewModel.haveSelect.set(false);
                }
            }
        });

        //软键盘搜索按钮监听
        binding.searchEt.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH){
                initData();

                return true;
            }
            return false;
        });

    }

    @Override
    public void initData() {
        super.initData();

        //获取好友列表
        viewModel.clearParams().setParams("cp_id",cp_id)
                .setParams("start_date",start_date);

        if(BaseUtil.isValue(viewModel.searchText.get())){
            viewModel.setParams("search_name",viewModel.searchText.get());
        }
        viewModel.requestData(Constants.PRIVITE_USER_ORDER);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.showDatas.observe(this, mList -> {
            adapter.setNewData(viewModel.mList.get());
        });

        viewModel.uc.selectDatas.observe(this, aVoid -> {
            String forResultStr = viewModel.createStr();

            Bundle bundle = new Bundle();
            bundle.putString("slectData",forResultStr);
            setResult(bundle);
        });
    }
}
